package com.yoomiito.app.ui.my.remain.withdraw

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.my.ApplyBean
import com.yoomiito.app.model.my.MyUserCenterInfo
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.utils.NetworkUtils
import com.yoomiito.app.utils.YMToast
import org.greenrobot.eventbus.EventBus

class WithdrawPresenter(appConfig: AppConfig) : BaseActPresenter<WithdrawActivity>(appConfig) {

    fun withdrawalMsg() {
        if (NetworkUtils.checkNetPermission(v)) {
            return
        }
        YmApi.getYmService()
                .withdrawalMsg()
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<ApplyBean>() {
                    override fun onSuccess(data: ApplyBean) {
                        v.setData(data.msg)
                    }

                    override fun onFail(error: ApiException) {
                        v.dismissLoading()
                    }
                })

    }


    fun withdrawApply(id : String, money : String, type : Int) {
        YmApi.getYmService()
                .withdrawApply(id,money,type)
                .compose<BaseModel<*>>(v.bindToLifecycle<BaseModel<*>>())
                .compose<BaseModel<*>>(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose<BaseModel<*>>(XApi.getScheduler<BaseModel<*>>())
                .subscribe(object : ApiSubscriber<BaseModel<*>?>(handleException()) {
                    override fun onSuccess(t: BaseModel<*>?) {
                        YMToast.showText("申请成功")
                        v.finish()

                        val eventMessage = EventMessage()
                        eventMessage.eventType = "withdraw"
                        EventBus.getDefault().post(eventMessage)
                    }

                    override fun onFail(error: ApiException) {
                        v.dismissLoading()
                    }
                })
    }

    fun getUserCenterData() {
        YmApi.getYmService()
                .userCenterInfo
                .compose<BaseModel<MyUserCenterInfo>>(v.bindToLifecycle<BaseModel<MyUserCenterInfo>>())
                .compose<MyUserCenterInfo>(XApi.getApiTransformerData<MyUserCenterInfo>())
                .compose<MyUserCenterInfo>(XApi.getScheduler<MyUserCenterInfo>())
                .subscribe(object : ApiSubscriber<MyUserCenterInfo?>(handleException()) {

                    override fun onSuccess(t: MyUserCenterInfo?) {
                        t?.let { v.setData1(it) }
                    }

                    override fun onFail(error: ApiException?) {

                    }


                })
    }

}
